package com.system.Interceptor;

import com.system.function.object.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SessionInterceptor implements HandlerInterceptor {
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
            throws Exception {
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {


        //从Session中获取在线的用户
        Object object = request.getSession().getAttribute("users");
       /* User object = new User();
        object.setName("测试");
        object.setLv("1");
        object.setId("2");
        request.getSession().setAttribute("users", object);*/


        String requestURI = request.getRequestURI();

        if(requestURI.contains("/user/login")
                || requestURI.contains("/user/userLogin")
                || requestURI.contains("/user/userRegister")
                || requestURI.contains("/user/add")
            ){
            return true;
        }
        if (null == object) {
            request.getRequestDispatcher("/").forward(request, response);
            return false;
        } else {
            request.setAttribute("userMessage",object);
            return true;
        }
    }
}
